#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Function:   使用鼠标点击确定截图的左上点和右下点,并截取为PIL图片文件
Author:     bear
Version:    2018-1-21 19:00
Contact:
"""

import PyHook3
import pythoncom
import win32api
from PIL import ImageGrab
import os
import time

"""
def on_mouse_event(event):
    # 监听鼠标事件
    print('MessageName:', event.MessageName)
    print('Message:', event.Message)
    print('Time:', event.Time)
    print('Window:', event.Window)
    print('WindowName:', event.WindowName)
    print('Position:', event.Position)
    print('Wheel:', event.Wheel)
    print('Injected:', event.Injected)
    print('---')

    # return True to pass the event to other handlers
    # return False to stop the event from propagating
    # 返回 True 以便将事件传给其它处理程序
    # 注意，这儿如果返回 False ，则鼠标事件将被全部拦截
    # 也就是说你的鼠标看起来会僵在那儿，似乎失去响应了
    return True
"""

"""
def on_keyboard_event(event):
    print('MessageName:', event.MessageName)
    print('Message:', event.Message)
    print('Time:', event.Time)
    print('Window:', event.Window)
    print('WindowName:', event.WindowName)
    print('Ascii:', event.Ascii, chr(event.Ascii))
    print('Key:', event.Key)
    print('KeyID:', event.KeyID)
    print('ScanCode:', event.ScanCode)
    print('Extended:', event.Extended)
    print('Injected:', event.Injected)
    print('Alt', event.Alt)
    print('Transition', event.Transition)
    print('---')

    # return True to pass the event to other handlers
    # return False to stop the event from propagating
    return True
"""


def on_mouse_left_down_event(event):
    # print 'Position_x:', event.Position[0]
    # print 'Position_y:', event.Position[1]
    global click_times, start_click_x, start_click_y, end_click_x, end_click_y
    click_times = click_times + 1
    if click_times == 1:
        start_click_x = event.Position[0]
        start_click_y = event.Position[1]
    if click_times == 2:
        end_click_x = event.Position[0]
        end_click_y = event.Position[1]
        # PostQuitMessage(exitCode) Post a quit message to an app.
        win32api.PostQuitMessage(0)
    # 返回 True 以便将事件传给其它处理程序
    # 注意，这儿如果返回 False ，则鼠标事件将被全部拦截
    # 也就是说你的鼠标看起来会僵在那儿，似乎失去响应了
    return False


def get_mouse_click():
    # create the hook mananger
    # 创建一个“钩子”管理对象
    hm = PyHook3.HookManager()
    # register two callbacks
    # 监听所有鼠标事件
    hm.MouseLeftDown = on_mouse_left_down_event
    # hm.MouseAllButtonsDown = on_mouse_event
    # hm.KeyDown = on_keyboard_event
    # hook into the mouse and keyboard events
    # 设置鼠标“钩子”
    hm.HookMouse()
    # hm.HookKeyboard()
    # 进入循环，如不手动关闭，程序将一直处于监听状态
    # Runs a message loop until a WM_QUIT message is received.
    pythoncom.PumpMessages()


def get_screenshots_by_mouse():
    global click_times, start_click_x, start_click_y, end_click_x, end_click_y
    click_times = 0
    print '请使用鼠标左键点击选取截图开始点和结束点'
    get_mouse_click()
    print '已拦截鼠标左键点击事件', click_times, '次'
    box = (start_click_x, start_click_y, end_click_x, end_click_y)
    img = ImageGrab.grab(box)
    img.show()
    return img


def main():
    screenshots = get_screenshots_by_mouse()
    image_name = os.path.dirname(os.path.abspath(__file__)) + '/resource/screen_' + \
                 time.strftime("%H_%M_%S", time.localtime()) + '.png'
    screenshots.save(image_name)

# test
# main()
